#!/usr/bin/env bash
cmd=$( basename "$0" )
img="hisi3516cv610:ebaina"
dockerName="ljs-hisi3516cv610-docker"

gpu=false
function usage() {
    echo "Usage: ${cmd} [-i | -n | -g | -h]" 
    echo "      启动docker"
    echo "      -i: <docker_img>:<docker_tag>, 缺省: ${img}"
    echo "      -n: docker容器名称，缺省: ${dockerName}"
    echo "      -g: 增加gpu支持"
    echo "      -h: 输出此信息"
    exit 1
}

while getopts "i:n:gh" arg
do
    case "${arg}" in 
        i)
            img="${OPTARG}"
            shift 2
            ;;
        n)
            dockerName="${OPTARG}"
            shift 2
            ;;
        g)
            gpu=true
            shift
            ;;
        *)
            usage
            ;;
    esac
done

if [ $# -gt 0 ]
then
    usage
fi

echo "Run image ${img}..."

hostDir="${HOME}/docker_homes/ebaina_home"
etcDir="${HOME}/sys/etc_ebaina"
user="1172"
group="502"
clientHome="/home/ebaina"
workDir="$HOME/work/hisi3516cv610"

echo run docker image ${img}

# 先删除旧的container
docker container rm -f ${dockerName} 2> /dev/null

if ${gpu}
then
    gpuOption='--gpus all' 
else
    gpuOption=''
fi

docker run \
    -u ${user}:${group} \
    ${gpuOption} \
    --net host \
    --rm \
    --shm-size=1024m \
    -v /usr/local/cuda-12.5:/usr/local/cuda-12.5 \
    -v ${etcDir}/group:/etc/group:ro \
    -v ${etcDir}/passwd:/etc/passwd:ro \
    -v ${etcDir}/shadow:/etc/shadow:ro \
    -v ${etcDir}/sudoers:/etc/sudoers \
    -v ${hostDir}:${clientHome} \
    -v ${HOME}/work/hisi3516cv610/Sample:${clientHome}/Sample \
    -v ${HOME}/work/hisi3516cv610/hi3516cv610_musl:${clientHome}/hi3516cv610_musl \
    -v ${HOME}/work/hisi3516cv610/amct_pytorch_sample:${clientHome}/amct_pytorch_sample \
    -v ${HOME}/bin:${clientHome}/bin \
    -v ${HOME}/exer:${clientHome}/exer \
    -v ${HOME}/download:${clientHome}/download \
    -v ${HOME}/datasets:${clientHome}/datasets \
    -v ${HOME}/tmp:${clientHome}/tmp \
    -w ${clientHome} \
    --name "${dockerName}" \
    -it \
    ${img}
